﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
//MicroDesigner
using Jc.MicroDesigner.Objects;

namespace Jc.MicroDesigner.Drawings.Tools
{
    /// <summary>
    /// Инструмент для перемещения элементов
    /// </summary>
    public class MoveTool : DrawingTool
    {
        private BaseElement _SelectedElement = null;
        private bool _IfMoved = false; // был ли элемент перемещён
        private bool MouseDowned = false;
        private ToolTip tip = null;

        //предыдущее положение указателя мыши
        private int _X = 0, _Y = 0;

        public override void OnMouseDown(DrawPanel panel, MouseEventArgs e)
        {
            BaseElement selement = panel.SearchElementByXY(e.X, e.Y);
            switch (e.Button)
            {
                case MouseButtons.Right:
                    if (selement == null)
                        panel.ShowAreaMenu(e.X, e.Y);
                    else
                    {
                        _SelectedElement = selement;
                        panel.ShowElementMenu(selement, e.X, e.Y);
                    }
                    break;

                case MouseButtons.Left:
                    if (selement != null)
                    {
                        MouseDowned = true;
                        _SelectedElement = selement;
                        _X = e.X;
                        _Y = e.Y;
                        panel.Cursor = Cursors.SizeAll;
                    }
                    break;

                /* [DEPRECATED]
                //показывает имя элемента в виде подсказки
                case MouseButtons.Middle:
                    BaseElement el = panel.SearchElementByXY(e.X, e.Y);
                    if (el != null)
                    {
                        string str = el.Name + " : " + el.GetType().Name;
                        tip = new ToolTip(panel.Container);
                        tip.UseAnimation = false;
                        tip.UseFading = false;
                        tip.ShowAlways = false;
                        tip.AutomaticDelay = 0;
                        tip.AutoPopDelay = 0;
                        tip.InitialDelay = 0;
                        tip.Show(str, panel, e.Location);
                    }
                    break;
                */
            }
        }

        public override void OnMouseMove(DrawPanel panel, MouseEventArgs e)
        {
            if ((_SelectedElement != null) && (MouseDowned))
            {
                _SelectedElement.DrawOptions.Left += (e.X - _X);
                _SelectedElement.DrawOptions.Top += (e.Y - _Y);
                _X = e.X;
                _Y = e.Y;
                _IfMoved = true;
                panel.Invalidate();
            }
        }

        public override void OnMouseUp(DrawPanel panel, MouseEventArgs e)
        {
            MouseDowned = false;
            panel.Cursor = this._ToolCursor;
            if (_IfMoved)
            {
                //выполняем рассоединение всех пинов
                if (_SelectedElement is CircuitElement)
                {
                    CircuitElement ce = _SelectedElement as CircuitElement;
                    for (int i = 0; i < ce.PINs.Count; i++)
                    {
                        ce.PINs[i].Disconnect();
                    }
                }
                //поддержка дискретного перемещения
                if ((panel.Discreteness > 0) && !(_SelectedElement is Wire))
                {
                    Point p = new Point(_SelectedElement.DrawOptions.Left, _SelectedElement.DrawOptions.Top);
                    Point _DiscreetPoint = panel.GetDiscreetCoord(p);
                    _SelectedElement.DrawOptions.Left = _DiscreetPoint.X;
                    _SelectedElement.DrawOptions.Top = _DiscreetPoint.Y;
                    panel.Invalidate();
                }
                panel.Update();
            }
            _IfMoved = false;
            if (tip != null)
            {
                tip.Hide(panel);
                tip.Dispose();
                tip = null;
            }
        }

        public MoveTool()
        {
            _Name = "Мышь";
        }
    }
}